package com.example.weather.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.weather.service.WeatherService;
import com.example.weather.util.AppException;
import com.example.weather.util.HttpClientUtil;
import com.example.weather.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @Author: MengXQ
 * @Date: 2021/8/23 21:21
 */
@Service
public class WeatherServiceImpl implements WeatherService {
    @Override
    public Optional<Integer> getTemperature(String province, String city, String county) {
        if(StringUtil.isBlank(province)){
            System.out.println("province 不能为空");
            new AppException("province 不能为空");
        }
        if(StringUtil.isBlank(city)){
            System.out.println("city 不能为空");
            new AppException("city 不能为空");
        }
        if(StringUtil.isBlank(county)){
            System.out.println("county 不能为空");
            new AppException("county 不能为空");
        }
        String provinceResult = HttpClientUtil.doGet("http://www.weather.com.cn/data/city3jdata/china.html", "UTF-8");
        JSONObject provinceObject =  JSONObject.parseObject(provinceResult);
        if (!provinceObject.isEmpty() && provinceObject.containsKey(province)){
            String cityResult = HttpClientUtil.doGet("http://www.weather.com.cn/data/city3jdata/provshi/"+province+".html", "UTF-8");
            JSONObject cityObject =  JSONObject.parseObject(cityResult);
            if (!cityObject.isEmpty() && cityObject.containsKey(city)){
                String countyResult = HttpClientUtil.doGet("http://www.weather.com.cn/data/city3jdata/station/"+province+city+".html", "UTF-8");
                JSONObject countyObject =  JSONObject.parseObject(countyResult);
                if (!countyObject.isEmpty() && countyObject.containsKey(county)){
                    //全部符合条件开始查询天气
                    //重试机制写在 RetryUtil里
                    String temp = getTemp(province,city,county);
                    return Optional.ofNullable(Double.valueOf(temp).intValue());
                }else {
                    System.out.println("county 代码不存在");
                    new AppException("county 代码不存在");
                }
            }else{
                System.out.println("city 代码不存在");
                new AppException("city 代码不存在");
            }
        }else {
            System.out.println("province 代码不存在");
            new AppException("province 代码不存在");
        }

        return Optional.empty();
    }

    public String getTemp(String province, String city, String county){
        String responseStr = HttpClientUtil.doGet("http://www.weather.com.cn/data/sk/"+province+city+county+".html","UTF-8");
        JSONObject jsonObject =  JSONObject.parseObject(responseStr);
        if (!jsonObject.isEmpty() && jsonObject.containsKey("weatherinfo")){
            JSONObject info = jsonObject.getJSONObject("weatherinfo");
            return info.getString("temp");
        }
        return null;
    }
}
